<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <title>Data Manage</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <%
        #set task id
        if(is.null(COOKIES$"task")){
        taskid <- format(Sys.time(), "%y%m%d%H%M%S")
        filepath <- file.path("/var/www/Rcloud/upload", taskid)
        dir.create(filepath)
        setCookie("task", taskid)
        #save(taskid, file=paste(filepath, "/", taskid, ".rda", sep=""))
        cat(paste("A new task created. (id:", taskid, ")"))
        }else{
        taskid <- unlist(strsplit(COOKIES$"task", "|", fixed=TRUE))[1]
        #taskid <- COOKIES$"task"
        filepath <- file.path("/var/www/Rcloud/upload", taskid)
        p<-paste("Task id: ", taskid, "<br>")
        cat(p)
        }
        %>
        <h3>Data Import</h3>
        From a formated flat file:<br>
        <form action=# id="form1" name="form1" encType="multipart/form-data" method="post" >
            <input type="file" id="file1" name="txt"><br>
            separated by
            <select name="sep">
                <option value=",">comma</option>
                <option value=";">semicolon</option>
                <option value="/t">tab</option>
                <option value="">space</option>
            </select><br>
            column names
            <select name="head">
                <option value="TRUE">true</option>
                <option value="FALSE">false</option>
            </select>
            row names
            <select name="rownames">
                <option value="NULL">NULL</option>
                <option value="first">First</option>
            </select><br>
            <INPUT type="submit" value="upload">
        </form>
        <%
        #upload file, save POST to listfile.rda
        if(!is.null(FILES$txt)){
        #filepath <- file.path("/var/www/Rcloud/upload", taskid)
        destination <- file.path(filepath,FILES$txt$name)
        file.copy(FILES$txt$tmp_name, destination, overwrite=TRUE)
        #cat(paste(FILES$txt$name,as.character(POST$sep)))
        listfile <- paste(filepath, "/listfile.rda", sep="")
        if(file.exists(listfile)){
        load(listfile)
        filelist <- c(filelist, list(POST))
        save(filelist, file=listfile)
        }else{
        filelist <- list(POST)
        save(filelist, file=listfile)
        }
        }
        %>
        From other data file:(not done)
        <form name="other" action="#" method="POST" enctype="multipart/form-data">
            <select name="foreign">
                <option value="excel">excel(xls)</option>
                <option value="spss">SPSS(por)</option>
                <option value="SAS">SAS(xpt)</option>
            </select>
            <input type="file" name="excel" value="" /><br>
            column names
            <select name="head">
                <option value="TRUE">true</option>
                <option value="FALSE">false</option>
            </select>
            row names
            <select name="rownames">
                <option value="TRUE">true</option>
                <option value="FALSE">false</option>
            </select><br>
            <input type="submit" value="upload" name="excel" />
        </form>
        <h3>Data list</h3>
        Data in your workspace:<br>
        <%
        listfile <- paste(filepath, "/listfile.rda", sep="")
        if(file.exists(listfile)){
        load(listfile)
        cat("<table border>")
        cat("<thead>")
        cat("<tr>")
        cat("<th>files</th>")
        cat("<th>check</th>")
        cat("<th>delete</th>")
        cat("<th>download</th>")
        cat("</tr>")
        cat("</thead>")
        for(i in 1:length(filelist)){        
        cat("<form method='GET' action='#'>")
        cat("<tr>")
        cat("<td>", filelist[[i]]$txt, "<td>")
        cat("<input type='radio' name='check' value=", i, "/> <td>")
        cat("<input type='radio' name='delete' value=", i, "/>")
        file <- paste("upload", taskid, filelist[[i]]$txt, sep="/")
        cat("<td>", "<a href=", file, ">link</a>", "<td>")
        cat("<input type='submit' value='submit' name='submit' />")
        cat("</tr>")
        cat("</form>")
        }
        cat("</table>")
        if(!is.null(GET)){
        if(!is.null(GET$check)){
        library(R2HTML)
        i <- as.numeric(GET$check)
        ifile <- file.path(filepath, filelist[[i]]$txt)
        idata <- read.table(ifile, header=as.logical(filelist[[i]]$head), sep=filelist[[i]]$sep)
        if(filelist[[i]]$rownames=="first"){
        rownames(idata) <- as.character(idata[,1])
        idata <- as.data.frame(idata[, -1])
        }
        HTML(idata, file=paste(ifile, ".html", sep=""))
        htmllink <- paste(paste("upload", taskid, filelist[[i]]$txt, sep="/"), ".html", sep="")
        cat("<a href=", htmllink, "target='_blank'>Click to check</a>")
        }
        if(!is.null(GET$delete)){
        i <- as.numeric(GET$delete)
        filelist <- filelist[-i]
        save(filelist, file=listfile)
        cat("The ", i, "th record has been deleted.", sep="")
        }
        }
        }else{
        cat("No file uploaded.")
        }
        %>
    </body>
</html>
